Module# 15: Java Utility                                                         Lecture#59: Miscellaneous Utilities

 

 

//   Example 59.1 : Simple StringTokenizer parsing

 

// Demonstrate simple parsing a text

import java.util.StringTokenizer;

class StringParsingDemo1{

    public static void main(String args[]){ 

      StringTokenizer st = new StringTokenizer(“Joy with Java"," "); 

     while (st.hasMoreTokens()) { 

               System.out.println(st.nextToken()); 

     } 

   } 

 

// Example 59.2 : StringTokenizer parsing with key

 

// Demonstrate StringTokenizer.

import java.util.StringTokenizer;

class STDemo{

     static String in = "title=Java: Data Structures;" +

                         "author=DS;" +  "publisher=NPTEL;" +  "copyright=2020";

     public static void main(String args[]) {

          StringTokenizer st = new StringTokenizer(in, "=;");

      while(st.hasMoreTokens()) {

                 String key = st.nextToken();

               String val = st.nextToken();

               System.out.println(key + "\t" + val);

         }

  }

}

 

// Example 59.3 : Getting the current date and time

 

// Show date and time using only Date methods.

import java.util.Date;

class DateDemo {

    public static void main(String args[]) {

    // Instantiate a Date object

    Date date = new Date();

    // display time and date using toString()

    System.out.println(date);

    // Display number of milliseconds since midnight, January 15, 2020 GMT

    long msec = date.getTime();

    System.out.println("Milliseconds since Jan. 15, 2020 GMT = " + msec);

  }

}

 

// Example 59.4 : Demonstration of several calendar methods

 

// Demonstrate Calendar

import java.util.Calendar;

class CalendarDemo {

    public static void main(String args[]) {

    // Create a calendar initialized with the

    // current date and time in the default

    // locale and timezone.

    Calendar calendar = Calendar.getInstance();

    // Display current time and date information.

    System.out.print("Date: ");

    System.out.print(calendar);

    System.out.print("Time: ");

    System.out.print(calendar.get(Calendar.HOUR) + ":");

    System.out.print(calendar.get(Calendar.MINUTE) + ":");

    System.out.println(calendar.get(Calendar.SECOND));

      // Set the time and date information and display it.

    calendar.set(Calendar.HOUR, 10);

    calendar.set(Calendar.MINUTE, 29);

    calendar.set(Calendar.SECOND, 22);

    System.out.print("Updated time: ");

    System.out.print(calendar.get(Calendar.HOUR) + ":");

    System.out.print(calendar.get(Calendar.MINUTE) + ":");

    System.out.println(calendar.get(Calendar.SECOND));

    }

}

 

// Example 59.5 : Demonstration of several calendar methods

 

// Program to demonstrate add() method of Calendar class

 

import java.util.*;

public class Calendar5 {

    public static void main(String[] args)  {

        // creating calendar object

        Calendar calendar = Calendar.getInstance();

        calendar.add(Calendar.DATE, -15);

        System.out.println("15 days ago: " + calendar.getTime());

        calendar.add(Calendar.MONTH, 4);

        System.out.println("4 months later: " + calendar.getTime());

        calendar.add(Calendar.YEAR, 2);

        System.out.println("2 years later: " + calendar.getTime());

    }

}

 

// Example 59.6 : Getting the current date and time 

// Demonstrate GregorianCalendar

import java.util.*;

  class GregorianCalendarDemo {

      public static void main(String args[]) {

    String months[] = {"Jan", "Feb", "Mar", "Apr",

               "May", "Jun", "Jul", "Aug",

               "Sep", "Oct", "Nov", "Dec"};

    int year;

    // Create a Gregorian calendar initialized

    // with the current date and time in the

    // default locale and timezone.

    GregorianCalendar gcalendar = new GregorianCalendar();

    // Display current time and date information.

    System.out.print("Date: ");

    System.out.print(months[gcalendar.get(Calendar.MONTH)]);

    System.out.print(" " + gcalendar.get(Calendar.DATE) + " ");

    System.out.println(year = gcalendar.get(Calendar.YEAR));

    System.out.print("Time: ");

    System.out.print(gcalendar.get(Calendar.HOUR) + ":");

    System.out.print(gcalendar.get(Calendar.MINUTE) + ":");

    System.out.println(gcalendar.get(Calendar.SECOND));

      // Test if the current year is a leap year

    if(gcalendar.isLeapYear(year)) {

         System.out.println("The current year is a leap year");

    }

    else {

      System.out.println("The current year is not a leap year");

    }

    }

}

 

// Example 59.7 : Currency method

 

// Demonstrate Currency.

import java.util.*;

class CurDemo {

    public static void main(String args[]) {

    Currency c;

    c = Currency.getInstance(Locale.US);

    System.out.println("Symbol: " + c.getSymbol());

    System.out.println("Default fractional digits: " +

    c.getDefaultFractionDigits());

  }

}